/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-06-28
 * Time: 10:43
 */
public class test5 {
    private static final int finalNumber =200;
    private static Object lock = new Object();
    private static int currentNum = 1;


    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while(currentNum<finalNumber){
                synchronized (lock){
                    // 奇数
                    while (currentNum%2!=0){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("Thread t1:"+currentNum);
                    currentNum++;
                    lock.notify();
                }
            }
        });

        Thread t2 = new Thread(()->{
            while (currentNum<finalNumber){
                synchronized (lock){
                    // 偶数
                    while (currentNum%2!=1){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("Thread t2:"+currentNum);
                    currentNum++;
                    lock.notify();
                }
            }
        });

        t1.start();
        t2.start();
    }
}
